package com.hymjweb.demo.hymj.basic_data_manage.xt_upload_file.mapper;

import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.basic_data_manage.xt_upload_file.bean.XtUploadFilePojo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Mapper
@Component
public interface XtUploadFileMapper {

    class XtUploadFileMapperProvider extends SQL{

        public String findCacheAll() {
            return new SQL() {{
                SELECT("mff.id, mff.file_title, mff.file_name");
                FROM("xt_upload_file mff");
            }}.toString();
        }

        public String findAll(Map map) {
            String sql= new SQL() {{
                SELECT("t.*");
                FROM("xt_upload_file t");
                if (map != null) {
                    String fileTitle= DataTypeUtil.toStringObject(map.get("fileTitle"));
                    if (fileTitle != null && !"".equals(fileTitle)) {
                        WHERE("t.file_title like " + DataTypeUtil.quoteAllLikeStr(fileTitle));
                    }
                }

            }}.toString();

            return sql;

        }


        public String findObjectById(String id) {
            String sql= new SQL() {{
                SELECT("t.*");
                FROM("xt_upload_file t");
                WHERE("t.id = #{id}");
            }}.toString();
            return sql;
        }

        public String insert(XtUploadFilePojo obj) {
            String sql= new SQL() {{
                // 向表中进行插入时不需要给出列名，插入的列会根据values进行动态设定，如果给列名会出错
                // INSERT_INTO("xt_dep_cate(id,name,create_time,.....)"); // 这种设定方法是错误的。
                INSERT_INTO("xt_upload_file");
                VALUES("id", "#{id}");
                VALUES("file_title", "#{fileTitle}");
                VALUES("file_name", "#{fileName}");
                VALUES("create_time", "#{createTime}");
                VALUES("create_user_id", "#{createUserId}");
                VALUES("update_time", "#{updateTime}");
                VALUES("update_user_id", "#{updateUserId}");
            }}.toString();

            return sql;
        }

        public String update(XtUploadFilePojo obj) {
            return new SQL() {{
                UPDATE("xt_upload_file mff");
                SET("mff.file_title = #{fileTitle}");
                SET("mff.file_name = #{fileName}");
                SET("mff.update_time = #{updateTime}");
                SET("mff.update_user_id = #{updateUserId}");
                WHERE("mff.id = #{id}");
            }}.toString();
        }

        public String delete(Map map) {
            String sql=new SQL() {{
                DELETE_FROM("xt_upload_file");
                WHERE("id = #{id}");
            }}.toString();
            return sql;
        }

    }

    @SelectProvider(type = XtUploadFileMapperProvider.class, method = "findCacheAll")
    @Results(id="xtUploadFileProviderCacheMapId",value = {
            @Result(column = "file_title", property = "fileTitle"),
            @Result(column = "file_name", property = "fileName")
    })
    List<XtUploadFilePojo> findCacheAll(Map map);


    @Results(id="xtUploadFileMapperId",value = {
            @Result(column = "id", property = "id"),
            @Result(column = "file_title", property = "fileTitle"),
            @Result(column = "file_name", property = "fileName"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "create_user_id", property = "createUserId"),
            @Result(column = "update_time", property = "updateTime"),
            @Result(column = "update_user_id", property = "updateUserId")
    })
    @SelectProvider(type = XtUploadFileMapperProvider.class, method = "findAll")
    List<XtUploadFilePojo> findAll(Map map);


    @ResultMap(value = "xtUploadFileMapperId")
    @SelectProvider(type = XtUploadFileMapperProvider.class, method = "findObjectById")
    XtUploadFilePojo findObjectById(@Param("id") String id);

    @InsertProvider(type = XtUploadFileMapperProvider.class, method = "insert")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(XtUploadFilePojo obj);

    @UpdateProvider(type = XtUploadFileMapperProvider.class, method = "update")
    int update(XtUploadFilePojo obj);


    @DeleteProvider(type = XtUploadFileMapperProvider.class, method = "delete")
    int delete(Map map);

}
